<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 左侧侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 左侧侧边栏菜单 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409fff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="isActivePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <!-- 图标 -->
              <i class="el-icon-location"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单列表
      menulist: [],
      // 字体图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 二级菜单的激活状态
      isActivePath: ''
    }
  },
  methods: {
    // 退出
    logout() {
      // 清空 token
      window.sessionStorage.removeItem('token')
      // 跳转
      this.$router.push('/login')
    },
    // 获取左侧菜单列表
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menulist = res.data
      // console.log(this.menulist)
    },
    // 侧边栏菜单折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 二级菜单的激活状态
    saveNavState(isActivePath) {
      // 把地址保存到sessionStorage
      window.sessionStorage.setItem('path', isActivePath)
      this.isActivePath = isActivePath
    }
  },
  created() {
    this.getMenuList()
    this.isActivePath = window.sessionStorage.getItem('path')
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;

  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // 清除自带的内padding
    padding-left: 0;
    background: #333744;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 10px;
        color: #fff;
        font-size: 20px;
      }
    }
  }

  .el-aside {
    background: #373d47;
    .el-menu {
      border-right: 0;
    }
  }

  .el-main {
    background: #eaedf1;
  }
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background: #4a5064;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  font-size: 10px;
  line-height: 22px;
  cursor: pointer;
}
</style>
